<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BMI指数测试器</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
            color: #333;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center; /* 将内容水平居中 */
            align-items: center; /* 将内容垂直居中 */
            height: 100vh; /* 设置高度为视口的高度 */
            background-image: url("img/2.webp");
            background-size: 100%; /* 背景图片大小 */
            background-position: center; /* 背景图片位置 */
        }

        .container {
            max-width: 500px;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            text-align: center; /* 内部文本居中 */
        }

        h2 {
            color: #007bff;
        }

        label {
            display: block;
            margin: 10px 0 5px;
            font-weight: bold;
        }

        input[type="number"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
        }

        input[type="button"] {
            width: 48%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin: 5px 1%;
        }

        input[type="button"]:hover {
            background-color: #0056b3;
        }

        #result {
            margin-top: 20px;
            padding: 10px;
            background-color: #e9ecef;
            border: 1px solid #ccc;
            border-radius: 4px;
            text-align: center;
            font-weight: bold;
        }

        a {
            margin-top: 20px;
            color: #007bff;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
    <script>
        function calculateBMI() {
            var weight = parseFloat(document.getElementById("weight").value);
            var height = parseFloat(document.getElementById("height").value) / 100; // 将身高从厘米换算为米
            var result = document.getElementById("result");

            // 检查输入是否在有效范围内
            if (isNaN(weight) || isNaN(height) || height <= 0 || weight <= 0 || weight < 5 || weight > 300 || height * 100 < 50 || height * 100 > 245) {
                result.innerHTML = "请输入有效的身高（50-245厘米）和体重（5-300公斤）!";
                return;
            }

            var bmi = weight / (height * height);
            var bmiCategory = "";

            if (bmi < 18.5) {
                bmiCategory = "偏瘦";
            } else if (bmi >= 18.5 && bmi < 24.9) {
                bmiCategory = "正常范围";
            } else if (bmi >= 25 && bmi < 29.9) {
                bmiCategory = "超重";
            } else {
                bmiCategory = "肥胖";
            }

            result.innerHTML = "您的BMI是: " + bmi.toFixed(1) + " (" + bmiCategory + ")";
        }

        function resetFields() {
            document.getElementById("weight").value = "";
            document.getElementById("height").value = "";
            document.getElementById("result").innerHTML = "";
        }
    </script>
</head>
<body>
    <div class="container">
        <h2>BMI指数测试器</h2>
        <label for="weight">请输入体重（单位：公斤，范围：5-300）:</label>
        <input type="number" id="weight" placeholder="如：70" min="5" max="300">

        <label for="height">请输入身高（单位：厘米，范围：50-245）:</label>
        <input type="number" id="height" placeholder="如：175" min="50" max="245">

        <input type="button" value="计算BMI" onclick="calculateBMI()">
        <p><input type="button" value="重置" onclick="resetFields()"></p>

        <div id="result"></div>
        <p><a href="ceshihe.html">点击返回首页</a></p>
        <p>© 2024 周露航</p>
    </div>
</body>
</html>
